<template>
  <div class="shop-container">
    <div class="header">
      <span class="title">精选好物</span>
      <input class="search" v-model="keyword" placeholder="大家都在搜：沙滩短裙" @keyup.enter="loadGoods" />
      <div class="actions">
        <button class="icon-btn" title="购物车" @click="router.push('/cart')">🛒</button>
        <button class="icon-btn" title="订单" @click="router.push('/orders')">📦</button>
      </div>
    </div>

    <div class="goods-grid">
      <div v-for="g in goods" :key="g.id" class="goods-card" @click="goDetail(g.id)">
        <img class="cover" :src="g.image_url" :alt="g.name" />
        <div class="info">
          <div class="name">{{ g.name }}</div>
          <div class="desc">{{ g.description?.slice(0, 24) }}</div>
          <div class="meta">
            <span class="price">¥{{ g.price }}</span>
            <span class="stock">库存 {{ g.stock }}</span>
          </div>
        </div>
      </div>
    </div>

    <div class="bottom-nav">
      <div class="nav-item" @click="router.push('/feed')">
        <span class="nav-icon">🏠</span>
        <span class="nav-label">首页</span>
      </div>
      <div class="nav-item active">
        <span class="nav-icon">🛒</span>
        <span class="nav-label">购物</span>
      </div>
      <div class="nav-item" @click="router.push('/publish')">
        <span class="nav-icon-blue">+</span>
        <span class="nav-label">发布</span>
      </div>
      <div class="nav-item" @click="router.push('/profile')">
        <span class="nav-icon">👤</span>
        <span class="nav-label">我的</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import axios from '@/api'

const router = useRouter()
const goods = ref([])
const keyword = ref('')

const loadGoods = async () => {
  const res = await axios.get('/goods/', { params: { limit: 50, offset: 0 } })
  let list = res.data || []
  if (keyword.value) {
    const k = keyword.value.trim()
    list = list.filter(i => i.name.includes(k) || (i.description || '').includes(k))
  }
  goods.value = list
}

const goDetail = (id) => {
  router.push(`/goods/${id}`)
}

onMounted(loadGoods)
</script>

<style scoped>
.shop-container {
  width: 100vw;
  min-height: 100vh;
  background: #f5f5f5;
  padding-bottom: 70px;
}
.header {
  position: sticky;
  top: 0;
  background: white;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid #f0f0f0;
  z-index: 5;
}
.actions { display:flex; gap:8px; margin-left:auto; }
.icon-btn { background:none; border:none; font-size:18px; cursor:pointer; }
.title {
  font-weight: 700;
  color: #333;
}
.search {
  flex: 1;
  height: 34px;
  border-radius: 17px;
  border: 1px solid #eee;
  padding: 0 12px;
  background: #fafafa;
}
.goods-grid {
  padding: 12px;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}
.goods-card {
  background: #fff;
  border-radius: 10px;
  overflow: hidden;
  cursor: pointer;
}
.cover {
  width: 100%;
  height: 150px;
  object-fit: cover;
  background: #f0f0f0;
}
.info {
  padding: 10px;
}
.name {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 6px;
}
.desc {
  font-size: 12px;
  color: #666;
  height: 32px;
}
.meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 6px;
}
.price {
  color: #ff4757;
  font-weight: 700;
}
.stock {
  font-size: 12px;
  color: #999;
}
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: white;
  display: flex;
  justify-content: space-around;
  align-items: center;
  padding: 6px 0;
  border-top: 1px solid #f0f0f0;
  z-index: 100;
}
.bottom-nav .nav-item { display:flex; flex-direction:column; align-items:center; gap:2px; font-size:11px; color:#666; cursor:pointer; flex:1; }
.bottom-nav .nav-item.active { color:#007AFF; }
.nav-icon { font-size:18px; }
.nav-icon-blue { width:40px; height:40px; background:#007AFF; color:#fff; border-radius:50%; display:flex; align-items:center; justify-content:center; font-size:24px; font-weight:bold; }
.nav-label { font-size:10px; }
</style>


